package com.swt.gap.controller;

import com.swt.gap.model.vo.FileClassCountVO;
import com.swt.gap.model.vo.fileclass.AddOrUpdateFileClassModel;
import com.swt.gap.model.vo.fileclass.UserFileClassVO;
import com.swt.gap.service.FileClassService;
import com.swt.gap.service.SysGroupService;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zqw
 * @date 2020/5/11 18:03
 */
@Api(tags = "200、文件分类")
@RestController
@RequestMapping("/file-class")
@Slf4j
public class FileClassController {

    @Autowired
    FileClassService fileClassService;
    @Autowired
    SysGroupService sysGroupService;

    @PutMapping("addOrUpdate")
    @ApiOperation(value = "新增或更新数据源类别信息", notes = "新增或更新数据源类别信息")
    @ApiImplicitParam(name = "addOrUpdateFileClassModel", dataType = "AddOrUpdateFileClassModel",paramType = "body", value = "新增或更新数据源类别信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 201, message = "创建成功"),
            @ApiResponse(code = 400, message = "错误请求"),
            @ApiResponse(code = 409, message = "名称重复"),
            @ApiResponse(code = 500, message = "操作失败")
    })
    public ResponseEntity addOrUpdateFileClass(@RequestBody AddOrUpdateFileClassModel addOrUpdateFileClassModel){

        //1、名称非空校验
        String fileClassName = addOrUpdateFileClassModel.getFileClassName().trim();
        int fileClassId1 = addOrUpdateFileClassModel.getFileClassId();
        int userId = UserLoginUtils.getUserId();
        if (StringUtils.isBlank(fileClassName)){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称不能为空");
        }

        if(fileClassName.length() > 20){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称字数不能超过20");
        }
        //2、名称重名校验
        int sameNameNumber = fileClassService.countSameName(userId,fileClassName,fileClassId1);
        if(sameNameNumber > 0){
            return ResponseEntity.status(HttpStatus.CONFLICT).body("名称重复");
        }
        int fileClassId = fileClassService.addOrUpdateFileClass(addOrUpdateFileClassModel,userId);
        if(fileClassId < 1){
            return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("操作失败");
        }
        if(addOrUpdateFileClassModel.getFileClassId() < 1){
            //新增
            return ResponseEntity.status(HttpStatus.CREATED).body(fileClassId);
        }
        return ResponseEntity.ok(fileClassId);
    }

    @DeleteMapping("/{fileClassId}")
    @ApiOperation(value = "删除一个数据源类别", notes = "删除一个数据源类别")
    @ApiImplicitParam(name = "fileClassId", dataType = "int",paramType = "path", value = "数据源类别信息id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 500, message = "删除失败"),
    })
    public ResponseEntity deleteFileClass(@PathVariable int fileClassId){
        int count = fileClassService.deleteFileClass(fileClassId);
        if(count < 1){
            return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("删除失败");
        }
        return ResponseEntity.ok(count);
    }

    /*@DeleteMapping("/delete-one")
    @Deprecated
    @ApiOperation(value = "删除一个数据源类别", notes = "删除一个数据源类别")
    @ApiImplicitParam(name = "fileClassId", dataType = "int",paramType = "query", value = "数据源类别信息id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 500, message = "删除失败"),
    })
    public ResponseEntity deleteFileClassOld(@RequestParam int fileClassId){
        int count = fileClassService.deleteFileClass(fileClassId);
        if(count < 1){
            return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("删除失败");
        }
        return ResponseEntity.ok(count);
    }*/

    @GetMapping("get-by-user")
    @ApiOperation(value = "查询用户下的数据类别列表", notes = "查询用户下的数据类别列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", dataType = "String",paramType = "query", value = "资源类别名称关键字"),
            @ApiImplicitParam(name = "fileClassId", dataType = "int",paramType = "query", value = "数据源类别信息id，如果要排除此类别，需提供类别id,不排除默认给空值"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "查询失败"),
    })
    public ResponseEntity getUserFileClassList(String keyword,Integer fileClassId){

        if (keyword != null){
            //处理关键字,如果关键字太长，截取前20(资源类型长度不超过20)
            keyword = keyword.trim();
            if(keyword.length() > 20){
                keyword = keyword.substring(0,20);
            }
        }

        int userId = UserLoginUtils.getUserId();
        List<Integer> userIds = new ArrayList<>();
        userIds.add(userId);
        List<UserFileClassVO> userFileClassVOList = fileClassService.getUserFileClassList(userIds, keyword,fileClassId);
        if(userFileClassVOList == null){
            return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("查询失败");
        }
        return ResponseEntity.ok(userFileClassVOList);
    }

    @GetMapping("/file-count")
    @ApiOperation(value = "按类别统计", notes = "按类别统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", dataType = "String",paramType = "query", value = "资源类别名称关键字"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "查询失败"),
    })
    public ResponseEntity<FileClassCountVO> countFileByClass(String keyword){
        if (keyword != null){
            //处理关键字,如果关键字太长，截取前20(资源类型长度不超过20)
            keyword = keyword.trim();
            if(keyword.length() > 20){
                keyword = keyword.substring(0,20);
            }
        }
        int userId = UserLoginUtils.getUserId();
        List<Integer> userIds = sysGroupService.listSameGroupUserByUserId(userId);

        return ResponseEntity.ok(fileClassService.countFileByClass(userIds, keyword, null));
    }


}
